package http

import (
	baseAuth "gitee.com/zaiqiang231/go-base-app/base_app/auth"
	"gitee.com/zaiqiang231/lovesport-user-service/app/config"
	"gitee.com/zaiqiang231/lovesport-user-service/app/http/handler"
	"gitee.com/zaiqiang231/lovesport-user-service/app/http/service"
	"gitee.com/zaiqiang231/lovesport-user-service/app/model"
	"github.com/gin-gonic/gin"
)

var (
	GlobalJwtAuth *baseAuth.JWTAuth
)

func InitRouter(g *gin.Engine) {

	g.Use(gin.Logger(), gin.Recovery())
	//跨域 设置
	g.Use(httpHandler.Cors())
	//验证码
	g.GET("/testArticle", httpHandler.GetTestArticleHandler)
	g.GET("/captcha", httpHandler.Captcha)
	g.POST("/login", httpHandler.LoginHandler)

	GlobalJwtAuth = &baseAuth.JWTAuth{
		SigningAlgorithm: "HS256",
		Key:              []byte(config.GlobalAppConfig.JwtConfig.Key),
		TokenLookup:      "header: Authorization, query: token, cookie: jwt",
		TokenHeadName:    "Bearer",
		IdentityKey:      config.AuthIdentityKey,
	}
	users := g.Group("/users")
	{
		users.POST("/create", service.OperationRecordFunc, httpHandler.UserCreate)
		users.Use(GlobalJwtAuth.TokenCheckFunc, service.OperationRecordFunc)
		users.GET("/getUserByName", httpHandler.UserGet)
		users.POST("/update", httpHandler.UserUpdate)
		users.DELETE("/delete", httpHandler.UserDelete)
		users.GET("/getSelfUserInfo", httpHandler.UserSelfGet)
		users.POST("/setUserAuthority", httpHandler.SetUserAuthority)
		users.POST("/setUserAuthorities", httpHandler.SetUserAuthorities)
		users.POST("/getUserList", httpHandler.GetUserList)
		users.POST("/setUserPassword", httpHandler.SetUserPassword)
	}

	policies := g.Group("/policies", httpHandler.Publish, GlobalJwtAuth.TokenCheckFunc, service.OperationRecordFunc)
	{
		policies.POST("", httpHandler.PolicyCreate)
		policies.GET("", httpHandler.PolicyList)
		policies.GET(":name", httpHandler.PolicyGet)
		policies.PUT(":name", httpHandler.PolicyUpdate)
		policies.DELETE(":name", httpHandler.PolicySoftDelete)
		policies.DELETE("delete/:name", httpHandler.PolicyDelete)
	}

	menu := g.Group("/menu", GlobalJwtAuth.TokenCheckFunc, service.OperationRecordFunc)
	{
		menuHandler := httpHandler.SysMenuHandler{}
		menu.GET("/getMenu", menuHandler.GetMenu)
		menu.GET("/getMenuList", menuHandler.GetMenuList)
		menu.POST("/getBaseMenuById", menuHandler.GetBaseMenuById)
		menu.POST("/addBaseMenu", menuHandler.AddBaseMenu)
		menu.POST("/deleteBaseMenu", menuHandler.DeleteBaseMenu)
		menu.POST("/updateBaseMenu", menuHandler.UpdateBaseMenu)
	}

	authority := g.Group("/authority", GlobalJwtAuth.TokenCheckFunc, service.OperationRecordFunc)
	{
		handler := httpHandler.SysAuthorityHandler{}
		authority.POST("/createAuthority", handler.CreateAuthority)
		authority.POST("/deleteAuthority", handler.DeleteAuthority)
		authority.PUT("/updateAuthority", handler.UpdateAuthority)
		authority.POST("/copyAuthority", handler.CopyAuthority)
		authority.POST("/getAuthorityList", handler.GetAuthorityList)
		authority.GET("/getAllAuthorityList", handler.GetAllAuthorityList)
	}

	api := g.Group("/api", GlobalJwtAuth.TokenCheckFunc, service.OperationRecordFunc)
	{
		handler := httpHandler.SysApiHandler{}
		api.POST("/getApiList", handler.GetApiList)
		api.POST("/createApi", handler.CreateApi)
		api.POST("/getApiById", handler.GetApiById)
		api.POST("/updateApi", handler.UpdateApi)
		api.POST("/deleteApi", handler.DeleteApi)
		api.DELETE("/deleteApisByIds", handler.DeleteApisByIds)
	}

	dictionary := g.Group("/dictionary", GlobalJwtAuth.TokenCheckFunc, service.OperationRecordFunc)
	{
		handler := httpHandler.SysDictionaryHandler{}
		dictionary.POST("/getList", handler.GetList)
		dictionary.POST("/deleteList", handler.DeleteList)
		dictionary.POST("/update", handler.Update)
		dictionary.POST("/create", handler.Create)
		dictionary.POST("/detail/getList", handler.DetailGetList)
		dictionary.POST("/detail/create", handler.DetailCreate)
		dictionary.POST("/detail/update", handler.DetailUpdate)
		dictionary.POST("/detail/deleteList", handler.DetailDeleteList)
	}

	operation := g.Group("/operation", GlobalJwtAuth.TokenCheckFunc)
	{
		handler := httpHandler.SysOperationRecordHandler{}
		operation.POST("/getList", handler.GetList)
		operation.POST("/deleteList", handler.DeleteList)
	}

	//静态地址 - 访问图片
	g.Static(config.GlobalAppConfig.UploadConfig.PicturePath, config.GlobalAppConfig.UploadConfig.PictureStorePath)
	upload := g.Group("/upload")
	{
		upload.Use(GlobalJwtAuth.TokenCheckFunc)
		handler := httpHandler.SysUploadHandler{}
		upload.POST("/picture", handler.UploadPicture)
	}

	if !service.ServiceApiInstance.HasData() {
		//第一次 没有数据的时候，把当前有的api 插入到数据库
		entities := make([]model.SysApi, 0)
		list := g.Routes()
		for _, info := range list {
			entities = append(entities, model.SysApi{
				Method:      info.Method,
				Path:        info.Path,
				ApiGroup:    info.Path,
				Description: info.Path,
			})
		}
		service.ServiceApiInstance.InitData(entities)
	}
}
